package com.yeqian.utils;

import net.sf.ehcache.Element;
import net.sf.ehcache.store.compound.ReadWriteCopyStrategy;

import java.io.Serializable;

/**
 * @author yeqian
 * @return
 * @date 2020/12/27 10:23
 * <p>
 * * 优化缓存策略
 * * 序列化要缓存的对象，否则设置  copyOnRead="true" copyOnWrite="true" 缓存对象的时候可能报序列化错误
 */

public class MyCopyStrategy implements ReadWriteCopyStrategy<Element> {


    @Override
    public Element copyForWrite(Element element, ClassLoader classLoader) {
        if (element != null) {
            Object temp = (Serializable) element.getObjectValue();
            return new Element(element.getObjectKey(), temp);
        }
        return element;
    }

    @Override
    public Element copyForRead(Element element, ClassLoader classLoader) {
        if (element != null) {
            Object temp = (Serializable) element.getObjectValue();
            return new Element(element.getObjectKey(), temp);
        }
        return element;
    }
}